<?php
/**
 * Created by PhpStorm.
 * User: CJJ
 * Date: 2017/8/23
 * Time: 17:15
 */
//管理员方法类


require_once ('../../SQLBase.php');
//require_once ($CDir.'/GongAn/admin/Common.php');

class AdminManager{

    //获取管理员信息
    function getAdminInfo(){
        $db=new SQLBase();
        $queryStr='SELECT * FROM admin;';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            while ($temp=$result->fetch_assoc()){
                $resultArrayAll['data'][]=$temp;
            }
        }
        return $resultArrayAll;
    }
   //跟新或者添加管理员
    function  UpdateAdmin($data){
        $db=new SQLBase();
        $queryStr='SELECT count(*) FROM admin WHERE admin_id='.$data['admin_id'].';';
        $result=$db->queryData($queryStr);
        $temp=$result->fetch_assoc();
        if($temp['count(*)']>=1){
            $queryStr='DELETE FROM admin WHERE admin_id='.$data['admin_id'].';';
            $result=$db->queryData($queryStr);
            if(!$result){
                $resultArrayAll['state']=-1;
            }else{
                $queryStr='INSERT INTO admin(admin_id,admin_pwd) VALUES('.$data['admin_id'].',"'.$data['admin_pwd'].'");';
                $result=$db->queryData($queryStr);
                if($result)
                    $resultArrayAll['state']=1;
            }
        }else{
            $resultArrayAll['state']=-1;
        }
        return $resultArrayAll;
    }
    //删除管理员
    function  deleteAdmin($admin_id){
        $db=new SQLBase();
        $queryStr='DELETE FROM admin WHERE admin_ID="'.$admin_id.'";';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
        }
        return $resultArrayAll;
    }
    //添加管理员
    function addAdmin($data) {
        $db = new SQLBase ();
        $queryStr = 'SELECT count(*) FROM admin WHERE admin_id=' . $data ['admin_id'] . ';';
        $result = $db->queryData ( $queryStr );
        $temp = $result->fetch_assoc ();
        if ($temp ['count(*)'] == 0) {
            $queryStr = 'INSERT INTO admin(admin_id,admin_pwd) VALUES(' . $data ['admin_id'] . ',"' . $data ['admin_pwd'] . '");';
            $result = $db->queryData ( $queryStr );
            if ($result)
                $resultArrayAll ['state'] = 1;
            else
                $resultArrayAll ['state'] = - 1;
        }else{
            $resultArrayAll ['state'] = - 1;
        }
        return $resultArrayAll;
    }
}